<?php
	$sql = new db();
	
	if ($_GET['step'] == 1)
	{
		$req = "SELECT Matricule FROM camion WHERE GLN_Transporteur =".$_SESSION['login'];
		$sql->query($req);
		$camion = array();
		while ($tmp = $sql->fetch_array())
			$camion[] = $tmp;
	}
	else if($_GET['step'] == 2)
	{	
		$req = "SELECT MAX(Num_Tournee) AS num_tour FROM tournee AS tour, camion AS ca WHERE tour.Matricule = ca.Matricule AND ca.GLN_transporteur=".$_SESSION['login'];
		$sql->query($req);
		if ($sql->num_rows() > 0)
			{
				$res = $sql->fetch_array();
				$num_tour = $res['num_tour'] + 1;
			}
		else
			$num_tour = 1;
		$req = "INSERT INTO tournee VALUES(NULL, '".$num_tour."', '0-0-0', '0-0-0', '0-0-0', '0','".$_POST['camion']."','0')";
		$sql->query($req);
		$req = "";
	}
	if ($_GET['step'] > 1)
	{
		$req = "SELECT Num_Tournee AS num_tour, Id_tournee FROM tournee AS tour, camion AS ca WHERE tour.Matricule = ca.Matricule AND ca.GLN_transporteur=".$_SESSION['login']." AND Num_Tournee = (SELECT MAX(Num_Tournee) FROM tournee AS tour, camion AS ca WHERE tour.Matricule = ca.Matricule AND ca.GLN_transporteur=".$_SESSION['login'].")";
		$sql->query($req);
		$res = $sql->fetch_array();
		$num_tour = $res['num_tour'];
		$id_tour = $res['Id_tournee'];
		$req = "SELECT Id_Commande FROM offre_transport WHERE Etat = 'A' AND GLN_Transporteur=".$_SESSION['login'];
		$sql->query($req);
		$offre = array();
		while ($res = $sql->fetch_array())
			$offre[] = $res['Id_Commande'];
		if ($_GET['step'] == 3)
		{
			$req = "SELECT typ.*, p.Id_palette FROM type_palette AS typ, colis AS co, ligne_commande AS li, preparation AS pr, palette AS p WHERE li.Id_Commande = ".$_POST['offre']." AND li.Id_Commande = pr.Id_Commande AND pr.Id_ordre_preparation = p.Id_Ordre_preparation AND p.Id_palette = co.Id_palette AND p.Id_type = typ.Id_type GROUP BY p.Id_palette";
			$sql->query($req);
			$volpal = $poidspal = 0;
			while ($res = $sql->fetch_array())
				{
					$volpal += $res['Hauter_Max'] * $res['Largeur_Max'] * $res['Longueur_Max'];
					$poidspal += $res['Poid_Brut_Max'] + $res['Poid_Support'];
				}
			if (!isset($_GET['cap_poids']) AND !isset($_GET['vol_cam']))
				{
					$req = "SELECT Capacite_Volume, Capacite_Poids FROM camion, type_camion AS tyc WHERE Matricule=".$_POST['camion']." AND camion.Id_caractere = tyc.Id_caractere";
					$sql->query($req);
					$res = $sql->fetch_array();
					$vol_cam = $res['Capacite_Volume'] * 1000000;
					$cap_poid = $res['Capacite_Poids'];
				}
			else
				{
					$vol_cam = $_GET['vol_cam'];
					$cap_poid = $_GET['cap_poid'];
				}
			if (($vol_cam - $volpal) > 0 AND ($cap_poid - $poidspal > 0))
				{
					$vol_cam -= $volpal;
					$cap_poid -= $poidspal;	
					$verif = "Le point de livraison a bien ete ajoute";
					$req = "INSERT INTO Livraison VALUES(NULL, '".$_POST['h_delai']."', '0-0-0', '".$_POST['date_prevue'].":".$_POST['heure']."', '".$_POST['offre']."', '".$id_tour."')";
					$sql->query($req);
				}
			else
				$verif = "Le camion ne permet pas de prendre cette livraison.";
		}
		if ($_GET['step'] != 4)
			{
				$req = "SELECT * FROM livraison WHERE Id_tournee=".$id_tour;
				$sql->query($req);
				if ($sql->num_rows() > 0)
					{
						$aff_offre = array();
						while ($res = $sql->fetch_array())
							$aff_offre[] = $res;
					}
			}
		else
			{
				$req = "SELECT MIN(Date_Livraison) AS date_min, MAX(ADDTIME(Date_Livraison, Delai_Livraison)) AS date_max, COUNT(*) AS nb_arret FROM livraison WHERE Id_tournee=".$id_tour;
				//echo $req;
				$sql->query($req);
				$res = $sql->fetch_array();
				$req = "UPDATE tournee SET Date_Ordre_Tournee = NOW(), Date_Debut = '".$res['date_min']."', Date_fin = '".$res['date_max']."', Nb_arret = ".$res['nb_arret']." WHERE Id_tournee=".$id_tour;
				$sql->query($req);
			}
		}
?>